﻿using System;
using System.Globalization;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Int64类型转换接口
    /// </summary>
    public class Int64TypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public Int64TypeConverter()
            : base(typeof(Int64))
        {

        }



        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="targetType">要待转换的目标类型</param>
        /// <param name="value">要转换的值</param>
        /// <param name="fromBase">值中数字基数(value是何种进制的字符串),必须是2,8,10,16</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(Type targetType, string value, byte fromBase = 10)
        {
            return System.Convert.ToInt64(value, fromBase);
        }

        /// <summary>
        /// 尝试将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="targetType">要待转换的目标类型</param>
        /// <param name="value">要转换的值</param>
        /// <param name="result">转换结果</param>
        /// <param name="style">字符串中数值样式[数字样式为NumberStyles值之一，日期时间为DateTimeStyles值之一,默认null为不使用样式]</param>
        /// <param name="formatProvider">一个提供区域性特定的格式设置信息的对象</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(Type targetType, string value, out object result, object style = null, IFormatProvider formatProvider = null)
        {
            Int64 targetResult;
            bool parseResult;
            if (style != null)
            {
                parseResult = Int64.TryParse(value, (NumberStyles)style, formatProvider, out targetResult);
            }
            else
            {
                parseResult = Int64.TryParse(value, out targetResult);
            }

            if (parseResult)
            {
                result = targetResult;
            }
            else
            {
                result = null;
            }
            return parseResult;
        }



#if CORE

        /// <summary>
        /// 尝试将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="targetType">要待转换的目标类型</param>
        /// <param name="value">要转换的值</param>
        /// <param name="result">转换结果</param>
        /// <param name="style">字符串中数值样式[数字样式为NumberStyles值之一，日期时间为DateTimeStyles值之一,默认null为不使用样式]</param>
        /// <param name="formatProvider">一个提供区域性特定的格式设置信息的对象</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(Type targetType, ReadOnlySpan<char> value, out object result, object style = null, IFormatProvider formatProvider = null)
        {
            Int64 Int64Result;
            bool parseResult;
            if (style != null)
            {
                parseResult = Int64.TryParse(value, (NumberStyles)style, formatProvider, out Int64Result);
            }
            else
            {
                parseResult = Int64.TryParse(value, out Int64Result);
            }

            if (parseResult)
            {
                result = Int64Result;
            }
            else
            {
                result = null;
            }
            return parseResult;
        }
#endif


    }
}
